﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace WebFormsBinder
{
    public static class ControlWrapperFinder
    {
        private static IEnumerable<ControlWrapperDefinition> _definitions;

        static ControlWrapperFinder()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            foreach (Type atype in assembly.GetTypes())
            {
                if (atype.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IControlWrapper<,>)))
                {
                }
            }

            _definitions =
                from type in assembly.GetTypes()
                where type.GetInterfaces().Any(x =>
                        x.IsGenericType &&
                        x.GetGenericTypeDefinition() == (typeof(IControlWrapper<,>))
                      )// &&
                //type.GetConstructor(BindingFlags.Public, null, System.Type.EmptyTypes, null) != null
                select new ControlWrapperDefinition(type,
                    type.GetInterfaces().First(x =>
                        x.IsGenericType &&
                        x.GetGenericTypeDefinition() == (typeof(IControlWrapper<,>))
                    ).GetGenericArguments()[0],
                    type.GetInterfaces().First(x =>
                        x.IsGenericType &&
                        x.GetGenericTypeDefinition() == (typeof(IControlWrapper<,>))
                    ).GetGenericArguments()[1]
                );

            //var a = from type in assembly.GetTypes()
            //        join iface in (
            //            from typeIface in type.GetInterfaces()
            //            where typeIface.IsGenerictype &&
            //                  typeIface.GetGenericTypeDefinition() == typeof(IControlWrapper<,>)
            //            select new { typeName = type.Name, gaA = 1, gaB = 2}
            //            ) on type.Name equals iface.typeName into joinedMess
            //        from jm in joinedMess
            //        select new ControlWrapperDefinition(type, typeIface.gaA, typeIface.gaB);

        }

        public static IControlWrapper<TProperty, TControl> GetControlWrapper<TProperty, TControl>()
        {
            var wrapper = _definitions.FirstOrDefault(
                x => x.TProperty == typeof(TProperty) &&
                    x.TControl == typeof(TControl));
            if (wrapper == null)
                throw new Exception("No wrapper for specified types exists.");

            Type wrapperType = wrapper.ControlWrapper;
            object instance = Activator.CreateInstance(wrapperType);
            return (IControlWrapper<TProperty, TControl>)instance;
        }

        class ControlWrapperDefinition
        {
            public Type TProperty { get; private set; }
            public Type TControl { get; private set; }
            public Type ControlWrapper { get; private set; }

            public ControlWrapperDefinition(Type type, Type propertyType, Type controlType)
            {
                //Type[] genericArgs = type.GetGenericArguments();
                //TProperty = genericArgs[0];
                //TControl = genericArgs[1];
                TProperty = propertyType;
                TControl = controlType;
                ControlWrapper = type;
            }
        }
    }
}
